package com.yuncheng.spcyApi.controller;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.spcyApi.busService.exam.constant.JudgementTypeEnum;
import com.yuncheng.spcyApi.busService.exam.service.ParseQuestionTextUtils;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.other.PxSjConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.common.base.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.common.StartEndTimeVo;
import com.yuncheng.spcyApi.vo.sj.SjjbxxVo;
import com.yuncheng.spcyApi.vo.sj.SjtmVo;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.*;

/**
 * @Description: 审评查验-培训-试卷基本信息
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-培训-试卷基本信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyPxSjjbxx")
public class SpcyPxSjjbxxController extends BaseController<SpcyPxSjjbxx, ISpcyPxSjjbxxService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyPxSjjbxxController.class);

    @Resource
    private ISpcyPxSjjbxxService spcyPxSjjbxxService;

    @Resource
    private ISpcyPxDjjbxxService spcyPxDjjbxxService;

    @Resource
    private ISpcyPxDjtmService spcyPxDjtmService;

    @Resource
    private ISpcyPxDjdaService spcyPxDjdaService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private ISpcyPxSjtmService spcyPxSjtmService;

    @Resource
    private ISpcyPxSjtmxxService spcyPxSjtmxxService;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;


    @Log(title = "审评查验-培训-试卷基本信息-分页列表查询")
    @ApiOperation(value = "审评查验-培训-试卷基本信息-分页列表查询")
    @GetMapping(value = "/sjjbxxPageList")
    public HttpResult<?> sjjbxxPageList(SpcyPxSjjbxx spcyPxSjjbxx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyPxSjjbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyPxSjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjjbxx.getFsjlx()),SpcyPxSjjbxx::getFsjlx,spcyPxSjjbxx.getFsjlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjjbxx.getFsjmc()),SpcyPxSjjbxx::getFsjmc,spcyPxSjjbxx.getFsjmc());
        queryWrapper.eq(spcyPxSjjbxx.getFjzrq() != null,SpcyPxSjjbxx::getFjzrq,spcyPxSjjbxx.getFjzrq());

        // 2023-6-26 新增
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null) {
            if (!SpcyConstant.Org_Code_Admin.equals(currentUser.getDeptCode())){
                queryWrapper.eq(SpcyPxSjjbxx::getSysOrgCode, currentUser.getDeptCode());
            }
        }

        queryWrapper.orderByDesc(SpcyPxSjjbxx::getCreateTime);
        IPage<SpcyPxSjjbxx> pageList = spcyPxSjjbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "答卷基本信息-分页列表查询")
    @ApiOperation(value = "答卷基本信息-分页列表查询")
    @GetMapping(value = "/djjbxxPageList")
    public HttpResult<?> djjbxxPageList(SpcyPxDjjbxx spcyPxDjjbxx,
                                        StartEndTimeVo startEndTimeVo,
                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyPxDjjbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyPxDjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFsjlx()),SpcyPxDjjbxx::getFsjlx,spcyPxDjjbxx.getFsjlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFsjmc()),SpcyPxDjjbxx::getFsjmc,spcyPxDjjbxx.getFsjmc());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFksxm()),SpcyPxDjjbxx::getFksxm,spcyPxDjjbxx.getFksxm());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFgzdw()),SpcyPxDjjbxx::getFgzdw,spcyPxDjjbxx.getFgzdw());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFlxdh()),SpcyPxDjjbxx::getFlxdh,spcyPxDjjbxx.getFlxdh());
        queryWrapper.like(StringUtils.isNotBlank(spcyPxDjjbxx.getFdf()),SpcyPxDjjbxx::getFdf,spcyPxDjjbxx.getFdf());
        if (startEndTimeVo.getStartTime() != null && startEndTimeVo.getEndTime() != null){
            queryWrapper.between(SpcyPxDjjbxx::getFtjsj,startEndTimeVo.getStartTime(),startEndTimeVo.getEndTime());
        }
        if (StringUtils.isNotBlank(startEndTimeVo.getFlag())) {
            if (startEndTimeVo.getFlag().equals("是")) {
                queryWrapper.isNotNull(SpcyPxDjjbxx::getFtjsj);
            } else {
                queryWrapper.isNull(SpcyPxDjjbxx::getFtjsj);
            }
        }

        // 2023-6-26 新增
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null) {
            if (!SpcyConstant.Org_Code_Admin.equals(currentUser.getDeptCode())){
                queryWrapper.eq(SpcyPxDjjbxx::getSysOrgCode, currentUser.getDeptCode());
            }
        }

//        queryWrapper.orderByDesc(SpcyPxDjjbxx::getFtjsj);
        queryWrapper.orderByDesc(SpcyPxDjjbxx::getCreateTime);
        IPage<SpcyPxDjjbxx> pageList = spcyPxDjjbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "修改答卷题目分数")
    @ApiOperation(value = "修改答卷题目分数")
    @PostMapping(value = "updateDjtmFs")
    public HttpResult<?> updateDjtmFs(@RequestBody SpcyPxDjtm djtm){
        if (StringUtils.isBlank(djtm.getId())){
            return HttpResult.error("答卷题目参数不能为空");
        }
        if (StringUtils.isBlank(djtm.getFtmdf())){
            return HttpResult.error("请填写分数");
        }
        SpcyPxDjtm updateDjtm = spcyPxDjtmService.getById(djtm.getId());
        if (updateDjtm == null){
            return HttpResult.error("当前题目不存在，请刷新重试");
        }
        String oldTmdf = updateDjtm.getFtmdf();
        updateDjtm.setFtmdf(djtm.getFtmdf());
        boolean flag = spcyPxDjtmService.updateById(updateDjtm);
        if (flag) {
            String log = "修改题目分数：【题目】->[" + updateDjtm.getFsjtm() + "];\n【分数】: [" + oldTmdf + "]修为[" + djtm.getFtmdf() + "]";
            spcyLogService.addLogs(LogConstant.LOGS_PX_DJTM, log, djtm.getFdjid(), SpcyConstant.LOGS_SYS_TYPE_XG, SpcyConstant.LOGS_SYS_TYPE_XG);
        }else {
            return HttpResult.error("保存失败，请刷新重试，并联系管理员");
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "重新计算答卷分数")
    @ApiOperation(value = "重新计算答卷分数")
    @ApiImplicitParam(name = "djid",value = "答卷id")
    @GetMapping(value = "reloadDjfs")
    public HttpResult<?> reloadDjfs(@RequestParam(value = "djid",defaultValue = "") String djid){
        if (StringUtils.isBlank(djid)){
            return HttpResult.error("答卷参数不能为空");
        }

        // 计算题目得分
//        spcyPxDjjbxxService.JsDjTmScore(djid);

        // 计算总得分
        spcyPxDjjbxxService.JsDjtmTotalScore(djid);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "审评查验-培训-试卷基本信息-添加")
    @ApiOperation(value = "审评查验-培训-试卷基本信息-添加")
    @PostMapping(value = "/addSjbxx")
    public HttpResult<?> addSjbxx(@RequestBody SpcyPxSjjbxx spcyPxSjjbxx) {
      String sjId = "";
      if (StringUtils.isNotBlank(spcyPxSjjbxx.getId())){
          sjId = spcyPxSjjbxx.getId();
          SpcyPxSjjbxx oldSjjbxx = spcyPxSjjbxxService.getById(sjId);
          spcyPxSjjbxxService.updateById(spcyPxSjjbxx);

          String operContent = "基本信息修改：";
          Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjjbxx);
          operContent += LogUtils.BeanModifyMsg(spcyPxSjjbxx,oldMap);

          spcyLogService.addLogs(LogConstant.LOGS_PX_SJJBXX,operContent,sjId,LogConstant.LOGS_PX_SJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
      }else {
          sjId = GetUuIdUtils.ReplaceUuId();
          SpcyPxSjjbxx sjjbxx = new SpcyPxSjjbxx();
          BeanUtil.copyProperties(spcyPxSjjbxx,sjjbxx);
          sjjbxx.setId(sjId);
          spcyPxSjjbxxService.save(sjjbxx); // 新增基本试卷信息

          String operContent = "新增信息：";
          // 将map里面的属性和值转String
          Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(sjjbxx);
          operContent += LogUtils.MapToStringMethod(beanMap);
          spcyLogService.addLogs(LogConstant.LOGS_PX_SJJBXX,operContent,sjId, LogConstant.LOGS_PX_SJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
      }
       SpcyPxSjjbxx sjjbxx = spcyPxSjjbxxService.getById(sjId);
       return HttpResult.ok(sjjbxx);
    }


    @Log(title = "审评查验-培训-试卷基本信息-编辑")
    @ApiOperation(value = "审评查验-培训-试卷基本信息-编辑")
    @ApiImplicitParam(name = "id",value = "试卷基本信息id",required = true)
    @PutMapping(value = "/editSjjbxx")
    public HttpResult<?> editSjjbxx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("试卷基本信息id不能为空");
        }
        String sjjbxxId = buffer.get("id");
        SpcyPxSjjbxx oldSjjbxx = spcyPxSjjbxxService.getById(sjjbxxId);
        SpcyPxSjjbxx oldSjjbxxUpdate = spcyPxSjjbxxService.getById(sjjbxxId);
        if (oldSjjbxxUpdate == null){
            return HttpResult.error("当前试卷基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjjbxxUpdate, buffer);
        spcyPxSjjbxxService.updateById(oldSjjbxxUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjjbxx);
        operContent += LogUtils.BeanModifyMsg(oldSjjbxxUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_PX_SJJBXX,operContent,sjjbxxId,LogConstant.LOGS_PX_SJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }


    @Log(title = "审评查验-培训-试卷基本信息-批量删除")
    @ApiOperation(value = "审评查验-培训-试卷基本信息-批量删除")
    @DeleteMapping(value = "/deleteBatchSjjbxx")
    public HttpResult<?> deleteBatchSjjbxx(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("参数为空！");
        }
        List<SpcyPxSjjbxx> list = spcyPxSjjbxxService.listByIds(StringPatternUtils.getListByIds(ids));
        if (CollectionUtils.isEmpty(list)){
            return HttpResult.error("没有相匹配的数据！");
        }
        String operContent = "";
        for (SpcyPxSjjbxx sjjbxx : list){
            // 删除试卷题目 和 选项
            List<SpcyPxSjtm> sjtmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                    .eq(SpcyPxSjtm::getFsjid, sjjbxx.getId()));

            spcyPxSjjbxxService.removeById(sjjbxx.getId()); // 删除试卷

            operContent = "";
            operContent = "删除试卷信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjjbxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_PX_SJJBXX,operContent,sjjbxx.getId(),LogConstant.LOGS_PX_SJJBXX,SpcyConstant.LOGS_SYS_TYPE_SC);

            // 不为空
            if (CollectionUtils.isNotEmpty(sjtmList)){
                for (SpcyPxSjtm sjtm : sjtmList){
                    spcyPxSjtmxxService.remove(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                            .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())); // 删除选项
                }
                spcyPxSjtmService.remove(new LambdaQueryWrapper<SpcyPxSjtm>()
                        .eq(SpcyPxSjtm::getFsjid, sjjbxx.getId()));
            }
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "清空试卷题目")
    @ApiOperation(value = "清空试卷题目")
    @DeleteMapping(value = "emptySjtm")
    public HttpResult emptySjtm(@RequestParam(value = "id",defaultValue = "") @ApiParam("试卷id") String sjid){

        List<SpcyPxSjtm> sjtmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, sjid));
        if (CollectionUtils.isNotEmpty(sjtmList)) {
            for (SpcyPxSjtm sjtm : sjtmList){
                spcyPxSjtmxxService.remove(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())); // 删除选项
            }
            spcyPxSjtmService.remove(new LambdaQueryWrapper<SpcyPxSjtm>()
                    .eq(SpcyPxSjtm::getFsjid, sjid));
        }
        return HttpResult.ok();
    }

    @Log(title = "审评查验-培训-试卷基本信息-通过id查询")
    @ApiOperation(value = "审评查验-培训-试卷基本信息-通过id查询")
    @ApiImplicitParam(value = "id",name = "试卷id",required = true)
    @GetMapping(value = "/querySjjbxxById")
    public HttpResult<?> querySjjbxxById(@RequestParam("id") String id) {
        SpcyPxSjjbxx spcyPxSjjbxx = spcyPxSjjbxxService.getById(id);
        return HttpResult.ok(spcyPxSjjbxx);
    }


    @Log(title = "审评查验-培训-试卷题目-分页列表查询")
    @ApiOperation(value = "审评查验-培训-试卷题目-分页列表查询")
    @GetMapping(value = "/sjtmPageList")
    public HttpResult<?> sjtmPageList(SpcyPxSjtm spcyPxSjtm, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyPxSjtm> page = new Page<>(pageNo, pageSize);
        IPage<SpcyPxSjtm> pageList = new Page<>();
        LambdaQueryWrapper<SpcyPxSjtm> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(spcyPxSjtm.getFsjid())) {
            queryWrapper.like(SpcyPxSjtm::getFsjid, spcyPxSjtm.getFsjid());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFsjtm()), SpcyPxSjtm::getFsjtm, spcyPxSjtm.getFsjtm());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFtmlx()), SpcyPxSjtm::getFtmlx, spcyPxSjtm.getFtmlx());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFtmfs()), SpcyPxSjtm::getFtmfs, spcyPxSjtm.getFtmfs());
            queryWrapper.orderByAsc(SpcyPxSjtm::getFxh);
            queryWrapper.orderByAsc(SpcyPxSjtm::getCreateTime);
            pageList = spcyPxSjtmService.page(page, queryWrapper);
        }

        return HttpResult.ok(pageList);
    }



    @Log(title = "审评查验-培训-试卷题目-添加")
    @ApiOperation(value = "审评查验-培训-试卷题目-添加")
    @PostMapping(value = "/addSjtm")
    public HttpResult<?> addSjtm(@RequestBody SpcyPxSjtm spcyPxSjtm) {
        if (StringUtils.isEmpty(spcyPxSjtm.getFsjid())){
            return HttpResult.error("试卷题目参数不能为空！");
        }
        String sjid = spcyPxSjtm.getFsjid();
        List<SpcyPxSjtm> sjtmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, sjid));
        int size = sjtmList.size();
        size = size + 1;
        spcyPxSjtm.setFxh(size);
        spcyPxSjtm.setId(GetUuIdUtils.ReplaceUuId());
        spcyPxSjtmService.save(spcyPxSjtm);

        /** 自动生成判断选项 */
        if (StringUtils.isNotBlank(spcyPxSjtm.getFtmlx())
                && spcyPxSjtm.getFtmlx().equals(PxSjConstant.SJTM_LX_PDT)) {
            List<SpcyPxSjtmxx> sjtmxxList = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                SpcyPxSjtmxx sjtmxx = new SpcyPxSjtmxx();
                sjtmxx.setId(GetUuIdUtils.ReplaceUuId());
                sjtmxx.setFsjtmid(spcyPxSjtm.getId());
                sjtmxx.setFxh(i + 1);
                sjtmxx.setFsfzqda(PxSjConstant.SJTM_DJDA_SFZQDA_NO);
                if (i == 0) {
                    sjtmxx.setFsjtmxx(JudgementTypeEnum.ZQ.getType());
                }else {
                    sjtmxx.setFsjtmxx(JudgementTypeEnum.CW.getType());
                }
                sjtmxxList.add(sjtmxx);
            }

            spcyPxSjtmxxService.saveBatch(sjtmxxList);
        }

        String operContent = "新增信息：";

        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxSjtm);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_PX_SJTM,operContent,spcyPxSjtm.getId(), LogConstant.LOGS_PX_SJTM, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }


    @Log(title = "审评查验-培训-试卷题目-编辑")
    @ApiOperation(value = "审评查验-培训-试卷题目-编辑")
    @ApiImplicitParam(name = "id",value = "试卷题目基本信息id",required = true)
    @PutMapping(value = "/editSjtm")
    public HttpResult<?> editSjtm(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("试卷题目基本信息id不能为空");
        }
        String sjtmId = buffer.get("id");
        SpcyPxSjtm oldSjtm = spcyPxSjtmService.getById(sjtmId);
        SpcyPxSjtm oldSjtmUpdate = spcyPxSjtmService.getById(sjtmId);
        if (oldSjtmUpdate == null){
            return HttpResult.error("当前试卷题目基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjtmUpdate, buffer);
        spcyPxSjtmService.updateById(oldSjtmUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjtm);
        operContent += LogUtils.BeanModifyMsg(oldSjtmUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_PX_SJTM,operContent,sjtmId,LogConstant.LOGS_PX_SJTM,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }


    @Log(title = "审评查验-培训-试卷题目-批量删除")
    @ApiOperation(value = "审评查验-培训-试卷题目-批量删除")
    @DeleteMapping(value = "/deleteBatchSjtm")
    public HttpResult<?> deleteBatchSjtm(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyPxSjtm> spcyRsGcyList = spcyPxSjtmService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyPxSjtm sjtm : spcyRsGcyList){
            String sjtmId = sjtm.getId(); // 试卷题目id
            spcyPxSjtmService.removeById(sjtmId);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjtm);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_PX_SJTM,operContent,sjtmId,LogConstant.LOGS_PX_SJTM,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";

            // 删除题目选项
            spcyPxSjtmxxService.remove(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                    .eq(SpcyPxSjtmxx::getFsjtmid, sjtmId));
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "审评查验-培训-试卷题目-通过id查询")
    @ApiOperation(value = "审评查验-培训-试卷题目-通过id查询")
    @ApiImplicitParam(value = "id",name = "试卷题目id",required = true)
    @GetMapping(value = "/querySjtmById")
    public HttpResult<?> querySjtmById(@RequestParam("id") String id) {
        SpcyPxSjtm spcyPxSjtm = spcyPxSjtmService.getById(id);
        return HttpResult.ok(spcyPxSjtm);
    }

    @Log(title = "试卷复制一份")
    @ApiOperation(value = "试卷复制一份")
    @GetMapping(value = "copySj")
    public HttpResult copySj(@RequestParam(value = "id",defaultValue = "") String id,
                             @RequestParam(value = "fsjmc",defaultValue = "") String fsjmc,
                             @RequestParam(value = "fsjlx",defaultValue = "") String fsjlx){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyPxSjjbxx oldsj = spcyPxSjjbxxService.getById(id);
        if (oldsj == null){
            return HttpResult.error("数据不存在");
        }

        SpcyPxSjjbxx newSj = new SpcyPxSjjbxx();
        BeanUtil.copyProperties(oldsj, newSj);

        newSj.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(fsjmc)) {
            newSj.setFsjmc("复制-" + newSj.getFsjmc());
        }else {
            newSj.setFsjmc(fsjmc);
        }

        if (StringUtils.isNotBlank(fsjlx)){
            newSj.setFsjlx(fsjlx);
        }

        newSj.setCreateTime(new Date());

        List<SpcyPxSjtm> newTmList = new ArrayList<>();
        List<SpcyPxSjtmxx> newXxList = new ArrayList<>();

        List<SpcyPxSjtm> oldTmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, oldsj.getId()));
        for (SpcyPxSjtm oldTm : oldTmList){

            SpcyPxSjtm newTm = new SpcyPxSjtm();
            BeanUtil.copyProperties(oldTm, newTm);
            newTm.setId(GetUuIdUtils.ReplaceUuId());
            newTm.setFsjid(newSj.getId());

            newTmList.add(newTm);

            List<SpcyPxSjtmxx> oldXxList = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                    .eq(SpcyPxSjtmxx::getFsjtmid, oldTm.getId()));
            for (SpcyPxSjtmxx oldXx : oldXxList){

                SpcyPxSjtmxx newXx = new SpcyPxSjtmxx();
                BeanUtil.copyProperties(oldXx, newXx);

                newXx.setId(GetUuIdUtils.ReplaceUuId());
                newXx.setFsjtmid(newTm.getId());

                newXxList.add(newXx);
            }

        }

        spcyPxSjjbxxService.save(newSj);
        spcyPxSjtmService.saveBatch(newTmList);
        spcyPxSjtmxxService.saveBatch(newXxList);

        return HttpResult.ok();
    }


    @Log(title = "审评查验-培训-试卷题目选项-分页列表查询")
    @ApiOperation(value = "审评查验-培训-试卷题目选项-分页列表查询")
    @GetMapping(value = "/sjtmxxPageList")
    public HttpResult<?> sjtmxxPageList(SpcyPxSjtmxx spcyPxSjtmxx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyPxSjtmxx> page = new Page<>(pageNo, pageSize);
        IPage<SpcyPxSjtmxx> pageList = new Page<>();
        LambdaQueryWrapper<SpcyPxSjtmxx> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(spcyPxSjtmxx.getFsjtmid())) {
            queryWrapper.like(SpcyPxSjtmxx::getFsjtmid, spcyPxSjtmxx.getFsjtmid());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtmxx.getFsjtmxx()), SpcyPxSjtmxx::getFsjtmxx, spcyPxSjtmxx.getFsjtmxx());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtmxx.getFsfzqda()), SpcyPxSjtmxx::getFsfzqda, spcyPxSjtmxx.getFsfzqda());
            queryWrapper.orderByAsc(SpcyPxSjtmxx::getFxh);
            queryWrapper.orderByAsc(SpcyPxSjtmxx::getCreateTime);
            pageList = spcyPxSjtmxxService.page(page, queryWrapper);
        }
        return HttpResult.ok(pageList);
    }

    @ApiOperation(value = "审评查验-培训-试卷题目选项-添加")
    @PostMapping(value = "/addSjtmxx")
    public HttpResult<?> addSjtmxx(@RequestBody SpcyPxSjtmxx spcyPxSjtmxx) {
        if (StringUtils.isEmpty(spcyPxSjtmxx.getFsjtmid())){
            return HttpResult.error("试卷题目id不能为空");
        }
        String sjtmid = spcyPxSjtmxx.getFsjtmid();
        List<SpcyPxSjtmxx> sjtmxxList = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                .eq(SpcyPxSjtmxx::getFsjtmid, sjtmid));
        int size = sjtmxxList.size();
        size = size + 1;
        spcyPxSjtmxx.setFxh(size);
        spcyPxSjtmxxService.save(spcyPxSjtmxx);

        String operContent = "新增信息：";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxSjtmxx);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_PX_SJTMXX,operContent,spcyPxSjtmxx.getId(), LogConstant.LOGS_PX_SJTMXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }


    @Log(title = "审评查验-培训-试卷题目选项-编辑")
    @ApiOperation(value = "审评查验-培训-试卷题目选项-编辑")
    @ApiImplicitParam(name = "id",value = "试卷题目选项基本信息id",required = true)
    @PutMapping(value = "/editSjtmxx")
    public HttpResult<?> editSjtmxx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("试卷题目选项基本信息id不能为空");
        }
        String sjtmId = buffer.get("id");
        SpcyPxSjtmxx oldSjtmxx = spcyPxSjtmxxService.getById(sjtmId);
        SpcyPxSjtmxx oldSjtmxxUpdate = spcyPxSjtmxxService.getById(sjtmId);
        if (oldSjtmxxUpdate == null){
            return HttpResult.error("当前试卷题目基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjtmxxUpdate, buffer);
        spcyPxSjtmxxService.updateById(oldSjtmxxUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjtmxx);
        operContent += LogUtils.BeanModifyMsg(oldSjtmxxUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_PX_SJTMXX,operContent,sjtmId,LogConstant.LOGS_PX_SJTMXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }


    @Log(title = "审评查验-培训-试卷题目选项-批量删除")
    @ApiOperation(value = "审评查验-培训-试卷题目选项-批量删除")
    @DeleteMapping(value = "/deleteBatchSjtmxx")
    public HttpResult<?> deleteBatchSjtmxx(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyPxSjtmxx> spcyRsGcyList = spcyPxSjtmxxService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyPxSjtmxx sjtmxx : spcyRsGcyList){
            String sjtmxxId = sjtmxx.getId(); // 试卷题目选项id
            spcyPxSjtmxxService.removeById(sjtmxxId);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjtmxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_PX_SJTMXX,operContent,sjtmxxId,LogConstant.LOGS_PX_SJTMXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";

        }

        return HttpResult.ok("删除成功");
    }


    @Log(title = "审评查验-培训-试卷题目选项-通过id查询")
    @ApiOperation(value = "审评查验-培训-试卷题目选项-通过id查询")
    @ApiImplicitParam(value = "id",name = "试卷题目选项id",required = true)
    @GetMapping(value = "/querySjtmxxById")
    public HttpResult<?> querySjtmxxById(@RequestParam("id") String id) {
        SpcyPxSjtmxx spcyPxSjtmxx = spcyPxSjtmxxService.getById(id);
        return HttpResult.ok(spcyPxSjtmxx);
    }

    @Log(title = "通过id查询试卷详细信息")
    @ApiOperation(value = "通过id查询试卷详细信息")
    @ApiImplicitParam(value = "id",name = "试卷id",required = true)
    @GetMapping(value = "/querySjxqxxById")
    public HttpResult<?> querySjxqxxById(@RequestParam("id")String id){
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("试卷id不能为空");
        }
        SjjbxxVo sjjbxxVo = new SjjbxxVo();

        SpcyPxSjjbxx sjjbxx = spcyPxSjjbxxService.getById(id);

        sjjbxxVo.setSjjbxx(sjjbxx); // 试卷基本信息

        // 单选题数据
        List<SpcyPxSjtm> sjtmDanXList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, id)
                .eq(SpcyPxSjtm::getFtmlx, PxSjConstant.SJTM_LX_DANX)
                .orderByAsc(SpcyPxSjtm::getFxh));

        List<SjtmVo> sjtmVoList1 = new ArrayList<>(); // 存放试卷题目和选项
        List<SpcyPxSjtmxx> sjtmxxList1 = new ArrayList<>();
        // 单选题
        if (CollectionUtils.isNotEmpty(sjtmDanXList)){
            for (SpcyPxSjtm sjtm : sjtmDanXList){
                SjtmVo sjtmVo = new SjtmVo();
                BeanUtil.copyProperties(sjtm,sjtmVo); // 存放题目
                // 题目选项
                 sjtmxxList1 = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())
                        .orderByAsc(SpcyPxSjtmxx::getFxh));
                 sjtmVo.setSjtmxxList(sjtmxxList1); // 存放选项
                sjtmVoList1.add(sjtmVo);
            }
        }

        // 多选题数据
        List<SpcyPxSjtm> sjtmDuoXList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, id)
                .eq(SpcyPxSjtm::getFtmlx, PxSjConstant.SJTM_LX_DUOX)
                .orderByAsc(SpcyPxSjtm::getFxh));

        List<SjtmVo> sjtmVoList2 = new ArrayList<>(); // 存放试卷题目和选项
        List<SpcyPxSjtmxx> sjtmxxList2 = new ArrayList<>();
        // 多选题
        if (CollectionUtils.isNotEmpty(sjtmDuoXList)){
            for (SpcyPxSjtm sjtm : sjtmDuoXList){
                SjtmVo sjtmVo = new SjtmVo();
                BeanUtil.copyProperties(sjtm,sjtmVo); // 存放题目
                // 题目选项
                sjtmxxList2 = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())
                        .orderByAsc(SpcyPxSjtmxx::getFxh));
                sjtmVo.setSjtmxxList(sjtmxxList2); // 存放选项
                sjtmVoList2.add(sjtmVo);
            }
        }

        // 主观题数据
        List<SpcyPxSjtm> sjtmZhugList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, id)
                .eq(SpcyPxSjtm::getFtmlx, PxSjConstant.SJTM_LX_ZHUG)
                .orderByAsc(SpcyPxSjtm::getFxh));

        List<SjtmVo> sjtmVoList3 = new ArrayList<>(); // 存放试卷题目和选项
        List<SpcyPxSjtmxx> sjtmxxList3 = new ArrayList<>();
        // 主观题数据
        if (CollectionUtils.isNotEmpty(sjtmZhugList)){
            for (SpcyPxSjtm sjtm : sjtmZhugList){
                SjtmVo sjtmVo = new SjtmVo();
                BeanUtil.copyProperties(sjtm,sjtmVo); // 存放题目
                // 题目选项
                sjtmxxList3 = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())
                        .orderByAsc(SpcyPxSjtmxx::getFxh));
                sjtmVo.setSjtmxxList(sjtmxxList3); // 存放选项
                sjtmVoList3.add(sjtmVo);
            }
        }

        // 判断题数据 2023-5-19 新增
        List<SpcyPxSjtm> sjtmPdtList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, id)
                .eq(SpcyPxSjtm::getFtmlx, PxSjConstant.SJTM_LX_PDT)
                .orderByAsc(SpcyPxSjtm::getFxh));

        List<SjtmVo> sjtmPdtVoList1 = new ArrayList<>(); // 存放试卷题目和选项
        List<SpcyPxSjtmxx> sjtmPdtxxList1 = new ArrayList<>();
        // 判断题
        if (CollectionUtils.isNotEmpty(sjtmPdtList)){
            for (SpcyPxSjtm sjtm : sjtmPdtList){
                SjtmVo sjtmVo = new SjtmVo();
                BeanUtil.copyProperties(sjtm,sjtmVo); // 存放题目
                // 题目选项
                sjtmPdtxxList1 = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, sjtm.getId())
                        .orderByAsc(SpcyPxSjtmxx::getFxh));
                sjtmVo.setSjtmxxList(sjtmPdtxxList1); // 存放选项
                sjtmPdtVoList1.add(sjtmVo);
            }
        }

        sjjbxxVo.setDanxList(sjtmVoList1);
        sjjbxxVo.setDuoxList(sjtmVoList2);
        sjjbxxVo.setZhugList(sjtmVoList3);
        sjjbxxVo.setPdtList(sjtmPdtVoList1); // 判断题

        return HttpResult.ok(sjjbxxVo);
    }

    /** 20240516*/
    @Log(title = "导出填写答卷详细信息文件PDF")
    @ApiOperation(value = "导出填写答卷详细信息文件PDF")
    @GetMapping(value = "exportDjFilePdf")
    public void exportDjFilePdf(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcyPxDjjbxx wj = spcyPxDjjbxxService.getById(id);
        if (wj == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的答卷数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        String destWordUrl = "";
        try {
            destWordUrl = this.initExportDjTemplate(wj, id);
        }catch (Exception e){
            log.error("导出答卷模板错误：{}", e.getMessage());
        }

        if (StringUtils.isBlank(destWordUrl)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("答卷模板错误！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        // pdf保存路径
        String destPdfUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".pdf";
        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);
        localFileUploadUtils.ejzLocalFileByZdyFile(new File(destPdfUrl), response);
    }

    @Log(title = "批量导出填写答卷PDF文件")
    @ApiOperation(value = "批量导出填写答卷PDF文件")
    @PostMapping(value = "exportDjFileZip")
    public void exportDjFileZip(@RequestBody ParamsBo paramsBo, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(paramsBo.getIds())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        List<SpcyPxDjjbxx> wjList = spcyPxDjjbxxService.listByIds(StringPatternUtils.getListByIds(paramsBo.getIds()));
        this.genExportDjFileZip(wjList, true, response);

    }

    @Log(title = "导出试卷模板PDF文件")
    @ApiOperation(value = "导出试卷模板PDF文件")
    @GetMapping(value = "exportSjPdfFile")
    public void exportSjPdfFile(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcyPxSjjbxx wj = spcyPxSjjbxxService.getById(id);
        if (wj == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的试卷数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        this.genExportSjFile(wj, true, response);
    }

    @Log(title = "导出试卷模板WORD文件")
    @ApiOperation(value = "导出试卷模板WORD文件")
    @GetMapping(value = "exportSjWordFile")
    public void exportSjWordFile(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcyPxSjjbxx wj = spcyPxSjjbxxService.getById(id);
        if (wj == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的试卷数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        this.genExportSjFile(wj, false, response);
    }

    /** 初始化试卷模板 */
    private String initExportSjTemplate(SpcyPxSjjbxx wj, String id){
        HashMap<String, Object> parMap = new HashMap<>();

        /** 试卷基本信息 */
        parMap.put("fsjmc", wj.getFsjmc());

        /** 试卷大标题 */
        List<Map<String, Object>> dbtListMap = new ArrayList<>(); // 大标题、题目及选项
        List<String> dbtList = CollectionUtil.newArrayList(PxSjConstant.SJTM_LX_DANX, PxSjConstant.SJTM_LX_DUOX, PxSjConstant.SJTM_LX_PDT);

        /** 组织试卷题目选项 */
        for (String dbt : dbtList) {
            Map<String, Object> dbrMap = new HashMap<>();

            /** 大标题 */
            if (dbt.equals(PxSjConstant.SJTM_LX_DANX)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFdanxtdbt()).orElseGet(()->"一、单选题"));
            }else if (dbt.equals(PxSjConstant.SJTM_LX_DUOX)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFduoxtdbt()).orElseGet(()->"二、多选题"));
            }else if (dbt.equals(PxSjConstant.SJTM_LX_PDT)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFpandtdbt()).orElseGet(()->"三、判断题"));
            }else {
                dbrMap.put("dbt", dbt);
            }

            // 题目及选项数据
            List<Map<String, Object>> questionsListMap = new ArrayList<>();

            /** 题目 */
            List<SpcyPxSjtm> tmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                    .eq(SpcyPxSjtm::getFsjid, id)
                    .eq(SpcyPxSjtm::getFtmlx, dbt)
                    .orderByAsc(SpcyPxSjtm::getFxh));

            for (SpcyPxSjtm tm : tmList) {
                Map<String, Object> questionMap = new HashMap<>();

                /** 题目相关信息 */
                questionMap.put("question", tm.getFsjtm());

                /** 题目选项 ☑ □ */
                String answer = "";
                List<SpcyPxSjtmxx> xxList = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                        .eq(SpcyPxSjtmxx::getFsjtmid, tm.getId())
                        .orderByAsc(SpcyPxSjtmxx::getFxh));
                String correntAnswer = "答案：";
                for (SpcyPxSjtmxx xx : xxList) {
                    answer += StringUtils.isNotBlank(answer) ? "\n" : "";

                    if (tm.getFtmlx().equals(PxSjConstant.SJTM_LX_PDT)){
                        // 判断题不处理
                    }else {
                        answer += xx.getFsjtmxx();
                    }

                    if (StringUtils.isNotBlank(xx.getFsfzqda())
                            && xx.getFsfzqda().equals(PxSjConstant.SJTM_DJDA_SFZQDA_YES)){
                        correntAnswer += ParseQuestionTextUtils.getOptionFirstChar(xx.getFsjtmxx(), '.');
                        if (tm.getFtmlx().equals(PxSjConstant.SJTM_LX_DUOX)){
                            correntAnswer += "；";
                        }
                    }
                }
                if (!tm.getFtmlx().equals(PxSjConstant.SJTM_LX_PDT)){
                    answer += "\n";
                }
                answer += correntAnswer;
                questionMap.put("answer", answer);

                questionsListMap.add(questionMap);
            }

            if (CollectionUtil.isEmpty(questionsListMap)){
                dbrMap.put("dbt", "");
            }
            dbrMap.put("questions", questionsListMap);

            dbtListMap.add(dbrMap);
        }
        parMap.put("dbtList", dbtListMap);

        String orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + WjmlConstant.Template_File_Export_SJJBXX;
        String destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + Util.getWjHz(orgUrl);

        ConfigureBuilder builder = Configure.builder();
        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, builder.build(), parMap);

        return destWordUrl;
    }

    /** 初始化答卷模板 */
    private String initExportDjTemplate(SpcyPxDjjbxx wj, String id){
        HashMap<String, Object> parMap = new HashMap<>();

        /** 试卷基本信息 */
        parMap.put("fsjmc", wj.getFsjmc());
        parMap.put("fdjzf", Optional.ofNullable(wj.getFdjzf()).orElseGet(()->"100") + "分");
        parMap.put("fdf", Optional.ofNullable(wj.getFdf()).orElseGet(()->"0") + "分");
        parMap.put("fksxm", wj.getFksxm());
        parMap.put("fgzdw", wj.getFgzdw());
        parMap.put("flxdh", wj.getFlxdh());
        parMap.put("ftxrq", wj.getCreateTime() != null ? DateConversionUtils.DateToYMDHMSChineseString(wj.getCreateTime()) : "");
        parMap.put("ftjrq", wj.getFtjsj() != null ? DateConversionUtils.DateToYMDHMSChineseString(wj.getFtjsj()) : "");

        /** 试卷大标题 */
        List<Map<String, Object>> dbtListMap = new ArrayList<>(); // 大标题、题目及选项
        List<String> dbtList = CollectionUtil.newArrayList(PxSjConstant.SJTM_LX_DANX, PxSjConstant.SJTM_LX_DUOX, PxSjConstant.SJTM_LX_PDT);

        /** 组织试卷题目选项 */
        for (String dbt : dbtList) {
            Map<String, Object> dbrMap = new HashMap<>();

            /** 大标题 */
            if (dbt.equals(PxSjConstant.SJTM_LX_DANX)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFdanxtdbt()).orElseGet(()->"一、单选题"));
            }else if (dbt.equals(PxSjConstant.SJTM_LX_DUOX)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFduoxtdbt()).orElseGet(()->"二、多选题"));
            }else if (dbt.equals(PxSjConstant.SJTM_LX_PDT)){
                dbrMap.put("dbt", Optional.ofNullable(wj.getFpandtdbt()).orElseGet(()->"三、判断题"));
            }else {
                dbrMap.put("dbt", dbt);
            }

            // 题目及选项数据
            List<Map<String, Object>> questionsListMap = new ArrayList<>();

            /** 题目 */
            List<SpcyPxDjtm> tmList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                    .eq(SpcyPxDjtm::getFdjid, id)
                    .eq(SpcyPxDjtm::getFtmlx, dbt)
                    .orderByAsc(SpcyPxDjtm::getFxh));

            for (SpcyPxDjtm tm : tmList) {
                Map<String, Object> questionMap = new HashMap<>();

                /** 题目相关信息 */
                questionMap.put("question", tm.getFsjtm());

                /** 题目选项 ☑ □ */
                String answer = "";
                List<SpcyPxDjda> xxList = spcyPxDjdaService.list(new LambdaQueryWrapper<SpcyPxDjda>()
                        .eq(SpcyPxDjda::getFdjtmid, tm.getId())
                        .orderByAsc(SpcyPxDjda::getFxh));
                for (SpcyPxDjda xx : xxList) {
                    answer += StringUtils.isNotBlank(answer) ? "\n" : "";
                    if (StringUtils.isNotBlank(xx.getFsfxz()) && xx.getFsfxz().equals(PxSjConstant.SJTM_DJDA_SFXZ_YES)) {
                        answer += "√" + xx.getFsjtmxx();
                    } else {
                        answer += "□" + xx.getFsjtmxx();
                    }
                }
                questionMap.put("answer", answer);

                questionsListMap.add(questionMap);
            }

            if (CollectionUtil.isEmpty(questionsListMap)){
                dbrMap.put("dbt", "");
            }
            dbrMap.put("questions", questionsListMap);

            dbtListMap.add(dbrMap);
        }
        parMap.put("dbtList", dbtListMap);

        String orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + WjmlConstant.Template_File_Export_DJJBXX;
        String destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + Util.getWjHz(orgUrl);

        ConfigureBuilder builder = Configure.builder();
        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, builder.build(), parMap);

        return destWordUrl;
    }

    /**
     * 批量导出填写答卷PDF文件
     * @param isPdf true: pdf
     * */
    private void genExportDjFileZip(List<SpcyPxDjjbxx> wjList, boolean isPdf, HttpServletResponse response){
        List<InputStream> files = new ArrayList<>();
        List<String> fileNames = new ArrayList();
        String destWordUrl = "";
        String wjccmc = "";
        String outFileUrl = "";
        String fileName = "";

        File fileMkdir = new File(localFileUploadUtils.getTempFileRootUrl() + "/" + DateConversionUtils.DateToMonthTwo(new Date()));
        if (!fileMkdir.exists()){
            fileMkdir.mkdirs();
        }

        for (SpcyPxDjjbxx wj : wjList){
            destWordUrl = "";
            wjccmc = "";
            outFileUrl = "";
            fileName = "";

            try {
                destWordUrl = this.initExportDjTemplate(wj, wj.getId());
            }catch (Exception e){
                log.error("导出答卷模板错误：{}", e.getMessage());
            }

            if (StringUtils.isBlank(destWordUrl)){
                continue;
            }

            if (isPdf) {
                /** pdf保存路径 */
                wjccmc = DateConversionUtils.DateToMonthTwo(new Date()) + "/" + GetUuIdUtils.NotReplaceUuId() + ".pdf";
                outFileUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + wjccmc;
                WordPdfUtils.wordToPdf(destWordUrl, outFileUrl);
            }else {
                outFileUrl = destWordUrl;
            }

            fileName += StringUtils.isNotBlank(wj.getFsjmc()) ? "("+wj.getFsjmc()+")" : "";
            fileName += StringUtils.isNotBlank(wj.getFgzdw()) ? (StringUtils.isNotBlank(fileName) ? "-"+wj.getFgzdw() : wj.getFgzdw()) : "";
            fileName += StringUtils.isNotBlank(wj.getFksxm()) ? (StringUtils.isNotBlank(fileName) ? "-"+wj.getFksxm() : wj.getFksxm()) : "";
            fileName += wj.getCreateTime() != null ? (StringUtils.isNotBlank(fileName) ? "-"+DateConversionUtils.DateToDays(wj.getCreateTime()) : DateConversionUtils.DateToDays(wj.getCreateTime())) : "";

            try {
                files.add(new FileInputStream(outFileUrl));
                if (isPdf) {
                    fileNames.add(fileName + ".pdf");
                }else {
                    fileNames.add(fileName + ".docx");
                }
            } catch (FileNotFoundException e) {

            }
        }

        PdfUtils.downInputZip(files, fileNames, response);
    }

    /**
     * 导出试卷模板文件
     * @param isPdf true: pdf
     * */
    private void genExportSjFile(SpcyPxSjjbxx wj, boolean isPdf, HttpServletResponse response) throws IOException {
        String destWordUrl = "";
        try {
            destWordUrl = this.initExportSjTemplate(wj, wj.getId());
        }catch (Exception e){
            log.error("导出试卷模板错误：{}", e.getMessage());
        }

        if (StringUtils.isBlank(destWordUrl)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("试卷模板错误！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        // pdf保存路径
        if (isPdf) {
            String destPdfUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".pdf";
            WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);
            localFileUploadUtils.ejzLocalFileByZdyFile(new File(destPdfUrl), response);
        }else {
            localFileUploadUtils.ejzLocalFileByZdyFile(new File(destWordUrl), response);
        }
    }
}
